﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DoAn.NhanVienKinhDoanh
{
    public partial class frmHoaDonBanHang : Form
    {
        private static MyDataDataContext _DC = new MyDataDataContext();
        public frmHoaDonBanHang()
        {

            InitializeComponent();
           
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmHoaDonBanHang_Load(object sender, EventArgs e)
        {
              
            ShowCombo();
            txtTenKH.Focus();
            
        }
        private void ShowCombo()
        { 
            var query= from h in _DC.HANGHOAs 
                        select new {h.Ma ,h.TenHangHoa} ;

            cmbTenHang.DataSource = query; 
            cmbTenHang.ValueMember = "Ma";
            cmbTenHang.DisplayMember = "TenHangHoa";
            

        }

        private int GetDonGia(int MaHangHoa)
        {
            var query = from h in _DC.HANGHOAs
                        where h.Ma ==MaHangHoa     
                        select new { h.DonGia };
            return query.AsEnumerable().ElementAt(0).DonGia;
        }

        private string  GetDVT(int MaHangHoa)
        {
            var query = from h in _DC.HANGHOAs
                        where h.Ma == MaHangHoa
                        select new { h.DonViTinh };
            return query.AsEnumerable().ElementAt(0).DonViTinh ;
        }

        private void CapNhatLuoi(DataGridView gridview )
        {
            int dongia=GetDonGia(int.Parse (cmbTenHang.SelectedValue.ToString ())  );
            string DVT=GetDVT(int.Parse(cmbTenHang.SelectedValue.ToString()));
            int soluong=int.Parse(txtSoLuong.Text);
          //them record
            gridview.Rows.Add(new object[] { int.Parse(cmbTenHang.SelectedValue.ToString()), cmbTenHang.Text, soluong, dongia,( soluong * dongia), DVT });
          
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            CapNhatLuoi(datagridChiTietHoaDon);
            CapNhatTongTien(datagridChiTietHoaDon,lblTongTien );
        }
        private void   CapNhatTongTien(DataGridView datagrid,Label lbltongtien)
        {
            int tt=0;
            int n=datagrid.RowCount ;
            while (n > 0)
            {
                tt += int .Parse ( datagrid.Rows[n - 1].Cells["colThanhTien"].Value.ToString());
                n--;
            }
            lbltongtien.Text = tt.ToString(); 
        }
    
        private void btnSua_Click(object sender, EventArgs e)
        {
            btnSua.Enabled = false;
        }
        private void datagridChiTietHoaDon_MouseClick(object sender, MouseEventArgs e)
        {
            btnSua.Enabled = true;
            
            
            
        }
       
        private bool  ThemHoaDon()
        {
            try
            {
                HOADON hoadon = new HOADON();
                hoadon.Ngay = DateTime.Now.Date;
                hoadon.TongTien = int.Parse(lblTongTien.Text);
                _DC.HOADONs.InsertOnSubmit(hoadon);
                _DC.SubmitChanges(); 
            }
            catch { return false; }
            return true;
             
        }
        private bool  ThemCTHD(DataGridView grid)
        {
            try
            {
                //ma hoa don
                int ma = _DC.HOADONs.AsEnumerable().ElementAt(_DC.HOADONs.Count()-1).Ma;
                //Ma Nhan Vien

                var query = from t in _DC.TAIKHOANs
                            join n in _DC.NHANVIENs on t.MaNhanVien equals n.Ma into tn
                            from n in tn.DefaultIfEmpty()
                            where t.MatKhau == MainForm.FrmDangNhap.Taikhoan.Mataikhoan
                                        && t.TenTaiKhoan == MainForm.FrmDangNhap.Taikhoan.Tentaikhoan
                            select new { n.Ma };
                int manv = query.AsEnumerable().ElementAt(0).Ma;
                for (int i = 0; i < grid.Rows.Count; i++)
                {
                    int mahanghoa = int.Parse(grid.Rows[i].Cells["colMaHang"].Value.ToString());
                    int soluong = int.Parse(grid.Rows[i].Cells["colSoLuong"].Value.ToString());
                    int thanhtien = int.Parse(grid.Rows[i].Cells["colThanhTien"].Value.ToString());

                    ThemCTHD(ma,manv,mahanghoa,soluong, thanhtien);
                }
            }
            catch
            { return false; }
            return true; 
        }
        private void  ThemCTHD(int mahoadon,int manhanvien,int mahanghoa,int soluong,int thanhtien)
        {
            CTHD ct = new CTHD();
            ct.MaHoaDon = mahoadon;
            ct.MaNhanVien = manhanvien;
            ct.MaHangHoa = mahanghoa;
            ct.SoLuong = soluong;
            ct.ThanhTien = thanhtien;
            _DC.CTHDs.InsertOnSubmit(ct);
            _DC.SubmitChanges();
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (datagridChiTietHoaDon.Rows.Count > 0)
            {
                btnLuu.Enabled = false; 
                if (ThemHoaDon())
                    if (ThemCTHD(datagridChiTietHoaDon))
                        lblThongBao.Text = "lưu thành công";
                    else
                        lblThongBao.Text = "Lưu không thành công";
                else
                    lblThongBao.Text = "lưu không thành công";
            }
            else
                lblThongBao.Text = "Chưa có dữ liệu để thêm";

        }

        private void btnLamlai_Click(object sender, EventArgs e)
        {
            CapNhatform();

        }
        private void CapNhatform()
        {
            btnLuu.Enabled = true;
            txtSoLuong.Text = "";
            lblTongTien.Text  = "0";
            lblThongBao.Text = "";
            datagridChiTietHoaDon.Rows.Clear();
            txtTenKH.Text = "";
        }
    }
}
 